package servlet;

import java.io.IOException;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspFactory;
import javax.servlet.jsp.PageContext;

import com.jspsmart.upload.Request;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import pojo.Product;
import service.ProductService;

/**
 * Servlet implementation class ProductServleta
 */
@WebServlet("/ProductServleta")
public class ProductServleta extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProductServleta() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");
		String method=request.getParameter("method");
		if(method.equals("add")) {
			add(request,response);
		}
	}

	private void add(HttpServletRequest request, HttpServletResponse response) {
		// TODO Auto-generated method stub
		SmartUpload su = new SmartUpload();
		//鍒濆鍖栧伐浣�
		PageContext context = JspFactory.getDefaultFactory()
				.getPageContext(this, request, response, null, true, 8*1024, true);
		try {
			su.initialize(context);
			su.setMaxFileSize(2*1024*1024);
			su.setAllowedFilesList("jpg,gif,png");
			//鏂囦欢鏀惧埌smartupload瀵硅薄涓紝骞舵病鏈夊瓨鍏ョ鐩�
			su.upload();
			//鍏堣幏鍙栨枃浠舵暟鎹�
			com.jspsmart.upload.File file1 = su.getFiles().getFile(0); //鍙笂浼犱竴涓枃浠舵椂
			//閲嶆柊瀹氫箟鏂囦欢鐨勫悕绉�,浜х敓鐨勯殢鏈烘暟.鍚庣紑鍚嶏紙getFileExt锛�
			String filename = UUID.randomUUID()+"."+file1.getFileExt();
			System.out.println("servlet....."+filename);
			String path = request.getServletContext().getRealPath("/upload");
			java.io.File f = new java.io.File(path);
			if(!f.exists()) {
				f.mkdir();
			}
	        //涓婁紶 
			file1.saveAs(request.getServletContext().getRealPath("/upload/"+filename));
			//鑾峰彇闈炴枃浠舵暟鎹�
			Request rq  = su.getRequest();
			String pid=rq.getParameter("p_id");
			String ptype=rq.getParameter("p_type");
			String pName=rq.getParameter("p_name");
			int pquantity=Integer.parseInt(rq.getParameter("p_quantity"));
			float pPrice=Float.parseFloat(rq.getParameter("p_price"));
			String pDescription=rq.getParameter("p_description");
			Product p = new Product();
			p.setP_type(ptype);
			p.setP_id(pid);
			p.setP_name(pName);;
			p.setP_quantity(pquantity);
			p.setP_price(pPrice);
			p.setP_description(pDescription);
			p.setP_image("/upload/"+filename);
			//璋冪敤service
			ProductService pservice = new ProductService();
			int flag = pservice.add(p);
			System.out.println(flag);
			
		} catch (ServletException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SmartUploadException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}


